#include "picwidget.h"
#include "ui_picwidget.h"

PicWidget::PicWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PicWidget)
{
    ui->setupUi(this);
    //设置默认选中的项,把图片尺寸设置为32X32
    ui->radioButton_32->setCheckable(true);//可以被选中
    ui->radioButton_32->setChecked(true);//显示界面后默认选中这一个
    this->width = 32;
    this->height= 32;
}

PicWidget::~PicWidget()
{
    delete ui;
}

void PicWidget::on_pushButton_select_clicked()
{
    qDebug() << "on_pushButton_select_clicked";
    if (ui->radioButton_16->isChecked()) // 检查按钮是否被按下
    {
        qDebug() << "16X16";
        this->width = 16;//设置默认值
        this->height= 16;//设置默认值

    } else if (ui->radioButton_32->isChecked()) {
        qDebug() << "32X32";
        this->width = 32;//设置默认值
        this->height= 32;//设置默认值
    } else if (ui->radioButton_64->isChecked()) {
        qDebug() << "64X64";
        this->width = 64;//设置默认值
        this->height= 64;//设置默认值
    } else if (ui->radioButton_128->isChecked()) {
        qDebug() << "128X128";
        this->width = 128;//设置默认值
        this->height= 128;//设置默认值
    }

    fileName = QFileDialog::getOpenFileName(this,
                            tr("Open Image"), "/Pictures", tr("Image Files (*.png *.jpg *.bmp)"));
    qDebug() << "fileName="<< fileName;//获取到选择的文件名称
    if(fileName.isEmpty()){
        qDebug() << "文件名为空,获取文件失败";
        return;
    }
    ui->label_filename->setText(fileName);
    QFileInfo fileInfo(fileName);
    qDebug() << "fileInfo.fileName()="<<fileInfo.fileName();

    QPixmap map(fileName);//创建一个图片对象
    QPixmap newMap = map.scaled(width,height);
    ui->label_preview->setPixmap(newMap);

}


void PicWidget::on_radioButton_16_clicked()
{
    this->width = 16;//设置默认值
    this->height= 16;//设置默认值
}


void PicWidget::on_pushButton_preview_clicked()
{
    if (ui->radioButton_16->isChecked()) // 检查按钮是否被按下
    {
        qDebug() << "16X16";
        this->width = 16;//设置默认值
        this->height= 16;//设置默认值

    } else if (ui->radioButton_32->isChecked()) {
        qDebug() << "32X32";
        this->width = 32;//设置默认值
        this->height= 32;//设置默认值
    } else if (ui->radioButton_64->isChecked()) {
        qDebug() << "64X64";
        this->width = 64;//设置默认值
        this->height= 64;//设置默认值
    } else if (ui->radioButton_128->isChecked()) {
        qDebug() << "128X128";
        this->width = 128;//设置默认值
        this->height= 128;//设置默认值
    }


    ui->label_filename->setText(fileName);
    QFileInfo fileInfo(fileName);
    qDebug() << "fileInfo.fileName()="<<fileInfo.fileName();

    QPixmap map(fileName);//创建一个图片对象
    QPixmap newMap = map.scaled(width,height);
    ui->label_preview->setPixmap(newMap);
}


void PicWidget::on_pushButton_delete_clicked()
{
    ui->label_filename->clear();//清空label中的内容
    ui->label_preview->clear();//清空label中的内容
    this->fileName = "";
}


void PicWidget::on_pushButton_save_clicked()
{

    QString saveFileName = QFileDialog::getSaveFileName(this,
                                            tr("Save Image"), "/Pictures", tr("Image Files (*.ico)"));
    qDebug() << "saveFileName ="<< saveFileName ;
    QPixmap map(fileName);
    QPixmap newMap = map.scaled(width, height);
    newMap.save(saveFileName);
}

